// pagesProperty/pages/event/detail/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading:true,
    hasData: false,
    task:{},
    netWork:false,
    taskId:0,
    targetId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.taskId = parseInt(options.id)
    }
  },
  onShow(){
    this.onHttpInit();
  },  
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/plan/task/detail', {
      task_id: that.data.taskId,
    }, function (res) {
      console.log("获取任务详情", res)
      if (res.data.code == 0) {
        var task = res.data.data.task;        
        that.setData({
          hasData: true,
          task: task
        })
      }else{
        Toast.fail(res.data.msg)
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  goSurvey(e){
    let surveyId = e.currentTarget.dataset.item.surveyStandardId;
    wx.navigateTo({
      url: '../../survey/question/index?id=' + surveyId,
    })
  },
  scanCode: function (res) {
    var task = res.currentTarget.dataset.task;
    var item = res.currentTarget.dataset.item;
    
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    var estate_id = app.getStorageSync("estate_id");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {}
          var linkParam = qrcontent;
          var estate_id = 0;
          var qrtype = '';
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = common_Js.getQueryVariable(q);
            var linkParam = "qrtype=mer&no=" + json_param.no;
            if(qrcontent.indexOf('mer/pay') >= 0){//商家的线下支付码
              wx.navigateTo({ url: '/pages/pay/merOffline/index?no=' + json_param.no })
            } 
            else if(qrcontent.indexOf('wxmini/terminal') >= 0){//巡检设备
              let surveyId = item.surveyTemplateId;
              console.log('json_param.id',json_param.id)
              console.log('item.id',item.id)
              if(parseInt(json_param.id) == item.id){
                wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id + 'targetId='+item.id+'&id=' + surveyId })
              }else{
                Toast.fail('请扫码本任务内的设备二维码')
              }
            } 
            else if(qrcontent.indexOf('wxmini/floor') >= 0){//保洁的楼层
              let surveyId = item.surveyTemplateId;
              console.log('json_param.id',json_param.id)
              console.log('item.id',item.floorId)
              if(parseInt(json_param.id) == item.floorId){
                wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id +'&targetId=' + item.floorId + '&id=' + surveyId })
              }else{
                Toast.fail('请扫码本任务内的区域二维码')
              }
            } 
            else if(qrcontent.indexOf('wxmini/area') >= 0){//区域
              let surveyId = item.surveyTemplateId;
              console.log('json_param.id',json_param.id)
              console.log('item.id',item.id)
              if(parseInt(json_param.id) == item.id){
                wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id +'&targetId=' + 0 + '&id=' + surveyId })
              }else{
                Toast.fail('请扫码本任务内的区域二维码')
              }
            } 
            else if(qrcontent.indexOf('wxmini/patrol') >= 0){//巡更点
              let surveyId = item.point.surveyTemplateId;
              console.log('json_param.id',json_param.id)
              console.log('item.point.id',item.point.id)
              if(parseInt(json_param.id) == item.point.id){
                wx.navigateTo({ url: '/pagesProperty/pages/survey/question/index?taskId=' + task.id +'&targetId=' + item.point.id  + '&id=' + surveyId })
              }else{
                Toast.fail('请扫码本任务内的巡更点二维码')
              }
            } else{
              Toast.fail("当前二维码无效");
            }
          }else{
            if (qrcontent.indexOf('qrtype') < 0) {//加密过的数据
              qrcontent = CryptoJS.Decrypt(res.result)//解密
              console.log('解密：qrcontent',qrcontent)
              json_param = JSON.parse(qrcontent);
              qrtype = json_param["qrtype"];
              linkParam = "qrtype=" + json_param["qrtype"];
              if(qrcontent.indexOf('estate') >= 0){
                linkParam += "&id=" + json_param["id"] + "&estate_id="+ json_param["id"];
                estate_id = parseInt(json_param["id"]);
              }
            }
            console.log('linkParam',linkParam)
            var is_estate_indexof = qrcontent.indexOf('estate');
            var is_device_indexof = qrcontent.indexOf('device');
            if (is_estate_indexof >= 0) {
              this.estateOp(linkParam,estate_id)
            } else{
              Toast.fail('该二维码不属于该操作');
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
})